package br.com.j7.jdesk.ui.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import br.com.j7.jdesk.library.exception.RepositoryException;
import br.com.j7.jdesk.persistence.repository.ServiceTypeRepository;
import br.com.j7.jdesk.persistence.repository.impl.ServiceTypeRepositoryImpl;
import br.com.j7.jdesk.ui.bean.WebContext;

/**
 * 
 * @author Edivando Alves
 *
 */
public class ServiceTypeConverter extends WebContext implements Converter{

	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value) {
		if (value != null && !"".equals(value) ){            
			ServiceTypeRepository repository = new ServiceTypeRepositoryImpl();
			try {
				return repository.findByServiceTypeAndEnterprise(value, getSessionEnterprise());
			} catch (RepositoryException e) {
				throw new RuntimeException(e);
			}
		}
		return null; 
	}

	@Override
	public String getAsString(FacesContext context, UIComponent componente, Object value) {
		if(value != null)
			return value.toString();
		return null;	
	}
	
}
